package com.github.codemperor.core.token;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author codemperor
 */
public class CurrentUserInfo {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 唯一请求号, 网关生成，链接所有服务
     */
    private String requestNo;
    /**
     * 当前服务节点，由服务自己生成
     */
    private String spanId;
    /**
     * 上个服务节点，由服务自己生成
     */
    private String parentSpanId;
    /**
     * 用户id
     */
    private String userId;
    /**
     * 用户昵称
     */
    private String userName;
    /**
     * 用户邮箱
     */
    private String userEmail;
    /**
     * 用户角色
     */
    private List<String> userRole = new ArrayList<>();
    /**
     * 请求Ip
     */
    private String remoteHostAddress;
    /**
     * 请求uri
     */
    private String uri;
    /**
     * 方法类型
     */
    private String method;

    /**
     * 接口类型，open、inner、auth等, 不透传给下游
     */
    private String apiType;
    /**
     * api角色， 不透传给下游, 打日志用
     */
    private List<String> apiRoleList = new ArrayList<>();

    public CurrentUserInfo() {
    }

    public CurrentUserInfo(HttpServletRequest req) {
        this.requestNo = req.getHeader(HeaderConst.HEADER_REQUEST_NO);
        this.spanId = req.getHeader(HeaderConst.HEADER_SPAN_ID);
        this.parentSpanId = req.getHeader(HeaderConst.HEADER_PARENT_SPAN_ID);
        this.userId = req.getHeader(HeaderConst.HEADER_USER_ID);
        String userName = req.getHeader(HeaderConst.HEADER_USER_NAME);
        if (!StringUtils.isEmpty(userName)) {
            try {
                this.userName = URLDecoder.decode(userName, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                this.userName = userName;
                log.error("", e);
            }
        }
        this.userEmail = req.getHeader(HeaderConst.HEADER_USER_EMAIL);

        String roleStr = req.getHeader(HeaderConst.HEADER_USER_ROLE);
        if (null != roleStr && !"".equals(roleStr)) {
            this.userRole = Arrays.asList(req.getHeader(HeaderConst.HEADER_USER_ROLE).split(","));
        }

        this.remoteHostAddress = req.getHeader(HeaderConst.HEADER_USER_IP);
        this.uri = req.getHeader(HeaderConst.HEADER_USER_URI);
        this.method = req.getHeader(HeaderConst.HEADER_USER_URI_METHOD);
    }

    public String getRequestNo() {
        return requestNo;
    }

    public void setRequestNo(String requestNo) {
        this.requestNo = requestNo;
    }

    public String getSpanId() {
        return spanId;
    }

    public void setSpanId(String spanId) {
        this.spanId = spanId;
    }

    public String getParentSpanId() {
        return parentSpanId;
    }

    public void setParentSpanId(String parentSpanId) {
        this.parentSpanId = parentSpanId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public List<String> getUserRole() {
        return userRole;
    }

    public void setUserRole(List<String> userRole) {
        this.userRole = userRole;
    }

    public String getRemoteHostAddress() {
        return remoteHostAddress;
    }

    public void setRemoteHostAddress(String remoteHostAddress) {
        this.remoteHostAddress = remoteHostAddress;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getApiType() {
        return apiType;
    }

    public void setApiType(String apiType) {
        this.apiType = apiType;
    }

    public List<String> getApiRoleList() {
        return apiRoleList;
    }

    public void setApiRoleList(List<String> apiRoleList) {
        this.apiRoleList = apiRoleList;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"requestNo\":\"")
            .append(requestNo).append('\"');
        sb.append(",\"spanId\":\"")
            .append(spanId).append('\"');
        sb.append(",\"parentSpanId\":\"")
            .append(parentSpanId).append('\"');
        sb.append(",\"userId\":\"")
            .append(userId).append('\"');
        sb.append(",\"userName\":\"")
            .append(userName).append('\"');
        sb.append(",\"userEmail\":\"")
            .append(userEmail).append('\"');
        sb.append(",\"userRole\":")
            .append(userRole);
        sb.append(",\"remoteHostAddress\":\"")
            .append(remoteHostAddress).append('\"');
        sb.append(",\"uri\":\"")
            .append(uri).append('\"');
        sb.append(",\"method\":\"")
            .append(method).append('\"');
        sb.append(",\"apiType\":\"")
            .append(apiType).append('\"');
        sb.append(",\"apiRoleList\":")
            .append(apiRoleList);
        sb.append('}');
        return sb.toString();
    }
}
